import { Component, Injector, ViewChild } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import {
    ContentConfig, PagedContentInput
} from '@shared/service-proxies/service-proxies';
import { LazyLoadEvent } from 'primeng/api';
import { contentListComponent } from './content-list.component';
import { AppContentService } from '../app-content.Service';
import { IContentComponent, IGoRelatePageDto } from '../IContentComponent';
import { ContentDetailModalComponent } from '../detail/content-detail-modal.component';
import { ModalDirective } from 'ngx-bootstrap/modal';
import { OrderByModalComponent } from '@app/main/shared/order-by/order-by-modal.component';

@Component({
    selector: 'content-list-modal',
    templateUrl: './content-modal.component.html'
})
export class ContentModalComponent extends AppComponentBase implements IContentComponent{
    @ViewChild('contentListModal', { static: true }) modal: ModalDirective;


    @ViewChild('contentListTab') contentListTab: contentListComponent;
    @ViewChild('contentDetailModal', { static: true }) contentDetailModal: ContentDetailModalComponent;

    @ViewChild('orderByModal', { static: true }) orderByModal: OrderByModalComponent;  

    isLoading: boolean = false;

    kind: number;
    customKey: string;
    relateId: string;

    inputPara: PagedContentInput;

    permissionDic: { [key: string]: boolean } = {};

    pageConfig: ContentConfig;

    hiddenList: boolean = true;
    hiddenDetail: boolean = true;

    isModal: boolean = true;

    advancedFiltersAreShown: boolean = false;
    dataSynchronous: boolean = false;

    title: string;

    constructor(
        injector: Injector,
        private _contentService: AppContentService
    ) {
        super(injector);
        this.inputPara = new  PagedContentInput();
    }

    show(relateId: string, kind: number, customKey:string, title: string): void {
        this.inputPara = new  PagedContentInput();
        this.inputPara.infoState = '#';
        this.permissionDic = {};

        this.advancedFiltersAreShown = false;

        this.hiddenList = true;
        this.hiddenDetail = true;
        this.isLoading = false;
        this.isModal = true;

        this.kind = kind;
        this.customKey = customKey;
        this.relateId = relateId;

        this.title = title;
        this.dataSynchronous =  this.setting.getBoolean("App.Synchronous.DataSynchronous");
        this._contentService.initListPage(this, () => { this.modal.show(); });
    }

    getListData(event?: LazyLoadEvent) {
        if (event) {
            this._contentService.getList(this, event);
        } else {
            this._contentService.getList(this);
        }
    }
    
    reloadPage() {
        this._contentService.reloadPage(this);
    }

    goRelatePage(data: IGoRelatePageDto): void {
   
    }

    delete() {
        this._contentService.deleteItems(this);
    }

    changeState(state: boolean) {
        this._contentService.changeState(this, state);
    }

    // 内容单页自动切换为详细页
    gotoDetail(id: string) {
        this._contentService.openCreateOrEdit(this, id);
    }

    editModal(record: { [key: string]: any; }) {
        this._contentService.openCreateOrEdit(this, record.id);
    }


    create(): void {
        this._contentService.openCreateOrEdit(this, null);
    }

    close(): void {
        this.modal.hide();
    }

    swapOrder(){
        this._contentService.swapOrder(this);
    }

    synchorization(): void {
        this._contentService.synchorization(this);
    }
}
